﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL
{
    public class ProducerRepo
    {
        public IList<Producer> GetProducers()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.Producer.Where(x => x.IsDeleted == false).OrderBy(x => x.Name).ToList();
            }
        }

        public void AddProducer(Producer producer)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                producer.IsDeleted = false;
                dbContext.Producer.Add(producer);
                dbContext.SaveChanges();
            }
        }

        public void UpdateProducer(Producer producer)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                producer.IsDeleted = false;
                dbContext.Entry(producer).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public bool DeleteProducer(int producerId)
        {
            bool canDeleted = true;
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var product = dbContext.Product.Where(x => x.ProducerId == producerId).ToList();
                if (product.Count > 0)
                {
                    canDeleted = false;
                }
                else
                {
                    int returnValue = dbContext.DeleteProducer(producerId);
                    if (returnValue == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                return canDeleted;
            }
        }

    }
}
